package com.voxel.sense.infomsg.view.menu;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.interfaces.listener.onMyDialogItemClickListener;
import com.voxel.sense.infomsg.utils.GlideUtils;
import com.voxel.sense.infomsg.view.MenuBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenyiyao on 2017/11/3.
 */

public class FirmWarnMultipleSelectAdapter extends BaseAdapter {
    public List<MenuBean> mDataList;
    public Context mContext;
    private int mAllItemIconRes;//选中全部item 的图标
    private boolean mAddSelectAllItem = false; //一个选中全部的item
    private boolean mIsVisibleIcon = false;//是否显示item图标
    private onMyDialogItemClickListener mMyItemClickListener;

    public FirmWarnMultipleSelectAdapter(List<MenuBean> menuBeanList, Context context) {
        this.mContext = context;
        this.mDataList = menuBeanList;
    }

    public FirmWarnMultipleSelectAdapter(List<MenuBean> mMenuBeanList, Context mContext,
                                         boolean mAddSelectAllItem, int mAllItemIconRes,
                                         boolean mIsVisibleIcon) {
        this.mDataList = mMenuBeanList;
        this.mContext = mContext;
        this.mIsVisibleIcon = mIsVisibleIcon;
        this.mAllItemIconRes = mAllItemIconRes;
        this.mAddSelectAllItem = mAddSelectAllItem;
        if (mAddSelectAllItem)
            addSelectAllItem();
    }

    @Override
    public int getCount() {
        if (mDataList != null) {
            return mDataList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHodler holder;
        if (convertView == null) {
            holder = new ViewHodler();
            convertView = parent.inflate(mContext,
                    R.layout.dialog_multiple_select_menu_item, null);
            holder.select_icon = (ImageView) convertView.findViewById(R.id.iv_select_icon);
            holder.iv_desc_icon = (ImageView) convertView.findViewById(R.id.iv_desc_icon);
            holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
            convertView.setTag(holder);
        } else {
            holder = (ViewHodler) convertView.getTag();
        }

        MenuBean bean = mDataList.get(position);
        boolean select = bean.isSelect();
        holder.tv_desc.setText(bean.getmText());
        if (select) {//切换状态
            holder.tv_desc.setTextColor(mContext.getResources().getColor(R.color.menu_selected));
            GlideUtils.GlideLoadPicNoDefault(mContext, R.mipmap.map_tip_select_icon, holder.select_icon);
        } else {//未选状态
            holder.tv_desc.setTextColor(mContext.getResources().getColor(R.color.menu_unselected));
            GlideUtils.GlideLoadPicNoDefault(mContext, R.mipmap.map_tip_unselect_icon, holder.select_icon);
        }

        if (mIsVisibleIcon) {//是否显示icon
            if (mAddSelectAllItem) {
                //添加了一个item,所有要进行判断
                if (position == 0) {
                    holder.iv_desc_icon.setVisibility(View.VISIBLE);
                    GlideUtils.GlideLoadPicNoDefault(mContext, mAllItemIconRes, holder.iv_desc_icon);
                } else {
                    holder.iv_desc_icon.setVisibility(View.VISIBLE);
                    GlideUtils.GlideLoadPicNoDefault(mContext, mDataList.get(position).getmIconResId(), holder.iv_desc_icon);

                }
            } else {
                holder.iv_desc_icon.setVisibility(View.VISIBLE);
                GlideUtils.GlideLoadPicNoDefault(mContext, mDataList.get(position).getmIconResId(), holder.iv_desc_icon);
            }

        } else {
            holder.iv_desc_icon.setVisibility(View.GONE);
        }

        //点击事件
        ItemClickEvent(position, convertView, holder);
        return convertView;
    }

    private void ItemClickEvent(final int position, View convertView, final ViewHodler holder) {
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MenuBean bean = mDataList.get(position);
                boolean select = !bean.isSelect();
                if (mAddSelectAllItem) {//有一个选中全部的item
                    if (position == 0) {
                        for (MenuBean m : mDataList) {
                            m.setSelect(select);//改变所有的按钮
                        }
                    } else {
                        MenuBean firstItem = mDataList.get(0);
                        //由于第一项是全部按钮，若被选中时，当再点击其它项时，都应该还原状态
                        if (firstItem.isSelect()) {
                            firstItem.setSelect(false);
                        }
                        //点击按钮，改变状态
//                        refreshIcon(holder, select, position);
                        mDataList.get(position).setSelect(select);
                    }
                    notifyDataSetChanged();
                } else {
                    //点击按钮，改变状态
                    refreshIcon(holder, select, position);//改变单个按钮
                    mDataList.get(position).setSelect(select);
                }
                if (mMyItemClickListener != null) {
                    mMyItemClickListener.onItemClick(position, bean);
                }
            }
        });
    }

    private void refreshIcon(ViewHodler holder, Boolean aBoolean, int position) {
        if (aBoolean) {//切换状态
            holder.tv_desc.setTextColor(mContext.getResources().getColor(R.color.menu_selected));
            GlideUtils.GlideLoadPic(mContext, R.mipmap.map_tip_select_icon, holder.select_icon);
        } else {//未选状态
            holder.tv_desc.setTextColor(mContext.getResources().getColor(R.color.menu_unselected));
            GlideUtils.GlideLoadPic(mContext, R.mipmap.map_tip_unselect_icon, holder.select_icon);
        }
    }

    public void setOnMyItemClickListener(onMyDialogItemClickListener listener) {
        this.mMyItemClickListener = listener;
    }


    public void addSelectAllItem() {
        MenuBean bean = new MenuBean("全部", mAddSelectAllItem+"", true);
        if (mDataList == null) {
            mDataList = new ArrayList<>();
        }
        mDataList.add(0, bean);
    }

    public int getAllItemIconRes() {
        return mAllItemIconRes;
    }

    public void setAllItemIconRes(int allItemIconRes) {
        mAllItemIconRes = allItemIconRes;
    }

    public void setData(List<MenuBean> data) {
        mDataList.clear();
        mDataList.addAll(data);
        if (mAddSelectAllItem)
            addSelectAllItem();
    }

    public static class ViewHodler {
        public ImageView select_icon;
        public ImageView iv_desc_icon;
        public TextView tv_desc;
    }

    //获取选中的数据
    public List getSelectListData() {
        ArrayList list = new ArrayList();
        if (mAddSelectAllItem) {
            //添加了一个默认选全部的item
            for (int i = 1; i < mDataList.size(); i++) {
                MenuBean menuBean = mDataList.get(i);
                if (menuBean.isSelect()) {
                    list.add(menuBean);
                }
            }
        } else {
            //没有添加了一个默认选全部的item
            for (MenuBean bean : mDataList) {
                if (bean.isSelect()) {
                    list.add(bean);
                }
            }
        }
        return list;
    }

}
